package com.jsgt.greekfire.ui.main.invite

import android.content.Context
import android.graphics.Bitmap
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jsgt.greekfire.R

class InvitePosterAdapter : BaseQuickAdapter<InvitePoster, BaseViewHolder>(R.layout.item_invite_poster) {
    private var selectPosition = 0
    private var QRBitmap: Bitmap? = null
    override fun convert(
        helper: BaseViewHolder,
        item: InvitePoster
    ) {
        helper.setImageResource(R.id.imvPoster, item.image)
            .setVisible(
                R.id.imvSelected,
                helper.adapterPosition == selectPosition
            )
            .setImageBitmap(R.id.imvQ, QRBitmap)
        helper.getView<View>(R.id.rootView)
            .setOnClickListener { setSelectPosition(helper.adapterPosition) }
    }

    fun setSelectPosition(selectPosition: Int) {
        if (this.selectPosition != selectPosition) {
            val oldPosition = this.selectPosition
            this.selectPosition = selectPosition
            notifyItemChanged(oldPosition)
            notifyItemChanged(selectPosition)
        }
    }

    val posterView: View?
        get() = getViewByPosition(selectPosition, R.id.posterView)

    fun setQRBitmap(QRBitmap: Bitmap?) {
        this.QRBitmap = QRBitmap
        notifyDataSetChanged()
    }

}